package cn.itcast.demo;

import cn.itcast.demo.pojo.Heima63;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class SolrJSuperQuery {

    @Test
    public void testRangeQuery() throws SolrServerException {
        //访问到solr
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //范围的定义为:price:[2000 TO 3000]  查询2000到3000这个区间的内容，包含边界
        SolrQuery solrQuery = new SolrQuery("price:[* TO 199999]");

        QueryResponse queryResponse = solrServer.query(solrQuery);

        List<Heima63> beans = queryResponse.getBeans(Heima63.class);

        beans.forEach(bean -> {
            String id = bean.getId();

            String title = bean.getTitle();

            Long price = bean.getPrice();

            System.out.println("id:" + id + " title:" + title + " price:" + price);
        });
    }


    /**
     * AND  OR  NOT
     * <p>
     * AND
     *
     * @throws SolrServerException
     */
    @Test
    public void testBooleanQuery() throws SolrServerException {
        //访问到solr
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //范围的定义为:price:[2000 TO 3000]  查询2000到3000这个区间的内容，包含边界
        SolrQuery solrQuery = new SolrQuery("price:[* TO 199999] OR title:乐视");

        QueryResponse queryResponse = solrServer.query(solrQuery);

        List<Heima63> beans = queryResponse.getBeans(Heima63.class);

        beans.forEach(bean -> {
            String id = bean.getId();

            String title = bean.getTitle();

            Long price = bean.getPrice();

            System.out.println("id:" + id + " title:" + title + " price:" + price);
        });
    }


    /**
     * 模糊查询其实就是一种的相似匹配，也就是约等的关系，只不过最大能偏离两个字符
     * 要模糊需要打开允许偏离的开关,最大编辑距离默认为2，可以修改，在波浪线后直接加数字
     *
     * @throws SolrServerException
     */
    @Test
    public void testFuzzyQuery() throws SolrServerException {
        //访问到solr
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //范围的定义为:price:[2000 TO 3000]  查询2000到3000这个区间的内容，包含边界
        SolrQuery solrQuery = new SolrQuery("title:vcva~1");

        QueryResponse queryResponse = solrServer.query(solrQuery);

        List<Heima63> beans = queryResponse.getBeans(Heima63.class);

        beans.forEach(bean -> {
            String id = bean.getId();

            String title = bean.getTitle();

            Long price = bean.getPrice();

            System.out.println("id:" + id + " title:" + title + " price:" + price);
        });
    }


    @Test
    public void testSortQuery() throws SolrServerException {
        //访问到solr
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //
        SolrQuery solrQuery = new SolrQuery("title:手机");
        solrQuery.setSort("price", SolrQuery.ORDER.desc);

        QueryResponse queryResponse = solrServer.query(solrQuery);

        List<Heima63> beans = queryResponse.getBeans(Heima63.class);

        beans.forEach(bean -> {
            String id = bean.getId();

            String title = bean.getTitle();

            Long price = bean.getPrice();

            System.out.println("id:" + id + " title:" + title + " price:" + price);
        });
    }

    @Test
    public void testPageQuery() throws SolrServerException {

        int currentPage = 4;

        final int PAGE_SIZE = 6;

        int start = (currentPage - 1) * PAGE_SIZE;


        //访问到solr
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //
        SolrQuery solrQuery = new SolrQuery("title:手机");
        //分页查询参数
        solrQuery.setStart(start);
        solrQuery.setRows(PAGE_SIZE);

        QueryResponse queryResponse = solrServer.query(solrQuery);

        List<Heima63> beans = queryResponse.getBeans(Heima63.class);

        beans.forEach(bean -> {
            String id = bean.getId();

            String title = bean.getTitle();

            Long price = bean.getPrice();

            System.out.println("id:" + id + " title:" + title + " price:" + price);
        });
    }



    @Test
    public void testHighLightQuery() throws SolrServerException {

        //访问到solr
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //一次查询中，高亮字段可以有多个
        solrQuery.addHighlightField("title");
        solrQuery.setHighlightSimplePre("<em>");
        solrQuery.setHighlightSimplePost("</em>");

        QueryResponse queryResponse = solrServer.query(solrQuery);

        List<Heima63> beans = queryResponse.getBeans(Heima63.class);


        //获取高亮结果，双层map，第一层map的key就是当前文档的id，第二层map的key为高亮字段的名称，值为高亮字段的结果（List）
        //schea:中默认的字段的属性值可以有多值，所以javaApi的设计必须满足所有的可能
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();


        beans.forEach(bean -> {
            //文档id
            String id = bean.getId();


            String highLightTitle = highlighting.get(id).get("title").get(0);

           // String title = bean.getTitle();

            Long price = bean.getPrice();

            System.out.println("id:" + id + " title:" + highLightTitle + " price:" + price);
        });
    }

}
